<?php
namespace Com\Pay;
class XunDaPay{

    private $config = array();


    public function __construct($send_data){
        $this->config = $send_data;
    }

    /*生成支付连接*/
    public function getUrl($order_info){
        $price = $order_info['paymoney'];
        $notifyUrl = 'http://' . $_SERVER['HTTP_HOST'] . '/Api/NotifyCallback/XunDaPayNotify?mch_id=' . $this->config['mch_id'];
        $order_sn = $order_info['ordernumber'];
        $post_data = array(
            'orderCode' =>   $order_info['orderCode'],
            'amount'    =>  $price,
            'route' => "chongluan",
            "userid"  =>  $this->config['mch_id'],
            'pay_number' => $order_sn,
            'notify_url'    => $notifyUrl,
            'returnUrl' => $order_info['return_url']
        );

        $ContentSign = $this->setParam($post_data,$this->config['mch_key'],false); // 计算出Sign
        $post_data['sign'] = $ContentSign; // Sign签名
        $tar_url = "http://extman.kefupay.cn/pay/pay_mobile.action";
        $res = json_decode($this->https_request($tar_url,$post_data) , true);
        if($res['respCode'] == "000000"){
            return $res['payUrl'];
        }
        return '';
    }


    /* 调用微信提现接口 */
//    public function weixin_tixian($order_info){
//        $order_sn = $order_info['ordernumber'];
//        $post_data = array(
//            'orderCode' =>   "ns_withdraw",
//            "amount"  =>  24628,
//            "cardNo"  =>  "6228480626724740172",
//            "userid"  =>  $this->config['mch_id'],
//            "accName"  =>  "魏来",
//            "pay_number"  =>  $order_sn,
//            "notifyUrl"  =>  "http://www.1diantv.com/xundadaifu.php",
//        );
//        $ContentSign = $this->setParam($post_data,$this->config['mch_key'],false); // 计算出Sign
//        $post_data['sign'] = $ContentSign; // Sign签名
//        $tar_url = "http://extman.kefupay.cn/pay/pay_mobile.action";
//        $res = json_decode($this->https_request($tar_url,$post_data) , true);
//        dump($res);die;
//        if($res['respCode'] == "000000"){
//            return $res['payUrl'];
//        }
//        return '';
//    }


    /* 查询微信余额信息 */
//    public function BalanceQuery($order_info){
//        $order_sn = $order_info['ordernumber'];
//        $post_data = array(
//            // ytyl_userQuery  ns_balanceCheck ns_balanceCheck这个是查询微信的
//            'orderCode' =>   "ns_balanceCheck",
//            "userid"  =>  $this->config['mch_id'],
//        );
//        dump($post_data);
//        $ContentSign = $this->setParam($post_data,$this->config['mch_key'],false); // 计算出Sign
//        $post_data['sign'] = $ContentSign; // Sign签名
//        $tar_url = "http://extman.kefupay.cn/pay/pay_mobile.action";
//        $res = json_decode($this->https_request($tar_url,$post_data) , true);
//        dump($res);die;
//        if($res['respCode'] == "000000"){
//            return $res['payUrl'];
//        }
//        return '';
//    }



    private function setParam($param,$key,$IS_Notify){
        if(!$IS_Notify) {
            $md5s = array(
                'amount' => $param['amount'],
                'returnUrl' => $param['returnUrl'],
                'userid' => $param['userid'],
                'route' => $param['route']
            );

// accountName=测试&accountNo=6212260502006204916&amount=0.1&bankName=中国工商银行&type=gateway&userid=2355


//            $md5s = array(
//                'amount' => $param['amount'],
//                'cardNo' => $param['cardNo'],
//                'userid' => $param['userid'],
//                'accName' => $param['accName'],
//                'notifyUrl' => $param['notifyUrl'],
//            );
             // 这是调用微信提现接口的签名






//            $md5s = array(
////                'route' => $param['route'],
//                'userid' => $param['userid'],
//            );
            ksort($md5s);
        }else{
            $md5s = array(
                'pay_number' => $param['pay_number'],
                'orderId' => $param['orderId'],
                'status' => $param['status'],
                'amount' => $param['amount'],
            );
        }
        $str = "";
        foreach ($md5s as $k => $v) $str.= $k . '=' . $v . '&';
        $str1 = rtrim($str , '&');
        $sign = md5($str1 . $key);
        return $sign;
    }

    //回调方法，把返回数据处理成统一规定返回
    public function response(){
        $data = $_REQUEST;
        unset($data['mch_id']);
        $MySign = $this->setParam($data,$this->config['mch_key'],true);
        if($data['sign'] == $MySign){
            if($data['status'] == '000000'){
                return true;
            }else{
                // 失败
                return false;
            }
        }else{
            // 失败
            return false;
        }
    }


    private function https_request($url, $data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

        //参数不为空则为post请求

        if (!empty($data)){

            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

}
